package injections.example;

import injections.DefinedProperty;
import injections.converters.Converter;

import java.util.*;

/**
 * Created by rehak on 2/14/14.
 */
public class CustomConverter implements Converter {

    private static final Class<?>[] CLSS = new Class<?>[]{CustomConverterClass.class};
    private static final Set<Class<?>> SUP = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(CLSS)));

    @Override
    public Set<Class<?>> supportedClasses() {
        return SUP;
    }

    @Override
    public CustomConverterClass convertValue(List<DefinedProperty> props, Class<?> cls) {
        if (SUP.contains(cls)) {
            String param1 = null;
            int param2 = 0;
            for (DefinedProperty p : props) {
                if (p.getName().equals("param1")) {
                    param1 = (String) p.getValues().get(0).getVal();
                }
                if (p.getName().equals("param2")) {
                    param2 = (Integer) p.getValues().get(0).getVal();
                }
            }
            return new CustomConverterClass(param1, param2);
        }
        throw new RuntimeException("Unsupported class for conversion " + cls);
    }

}
